package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.utils.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/*员工*/
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * <p>Description: 员工登录</p>
     *
     * @author liuyiming
     * @date 2022-10-06 14:41
     * @version 1.0.0
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request , @RequestBody Employee emp){
        //将页面提交的密码进行md5加密处理
        String password = Md5Utils.md5Password(emp.getPassword());
        //根据用户名查询用户信息
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,emp.getUsername());
        queryWrapper.eq(Employee::getPassword,password);
        Employee empInfo = employeeService.getOne(queryWrapper);
        //判断查询的数据是否为空
        if (empInfo == null){
            return R.error("账号或密码错误");
        }
        //判断员工状态是否为禁用
        if (empInfo.getStatus() == 0){
            return R.error("该账号已封禁");
        }
        //登陆成功
        request.getSession().setAttribute("employee",empInfo.getId());
        return R.success(empInfo);
    }

    /**
     * <p>Description: 员工退出</p>
     *
     * @author liuyiming
     * @date 2022-10-06 14:48
     * @version 1.0.0
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        //删除Session中保存的当前登录员工的id
        session.removeAttribute("employee");
        //清空session中所有内容
        //session.invalidate();
        return R.success("退出成功");
    }

    /**
     * <p>Description: 新增员工</p>
     *
     * @author liuyiming
     * @date 2022-10-07 10:55
     * @version 1.0.0
     */
    @PostMapping
    public R<String> save(HttpSession session,@RequestBody Employee emp){
        log.info("新增员工，员工信息：{}",emp);
        //设置初始密码123456
        emp.setPassword( Md5Utils.md5Password("123456"));
        //设置审计字段
        //emp.setCreateTime(LocalDateTime.now());
        //emp.setUpdateTime(LocalDateTime.now());
        //emp.setCreateUser((Long) session.getAttribute("employee"));
        //emp.setUpdateUser((Long) session.getAttribute("employee"));

        employeeService.save(emp);
        return R.success("添加员工成功");
    }

    /**
     * <p>Description: 分页查询员工列表</p>
     *
     * @author liuyiming
     * @date 2022-10-07 11:15
     * @version 1.0.0
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        log.info("员工列表分页查询page={},pageSize={},name={}",page,pageSize,name);
        //分页构造器
        Page pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //名称模糊分页查询
        queryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);
        //根据修改时间字段倒序排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * <p>Description: 根据ID修改员工信息</p>
     *
     * @author liuyiming
     * @date 2022-10-07 15:39
     * @version 1.0.0
     */
    @PutMapping
    public R<String> updateById(HttpSession session,@RequestBody Employee emp){
        log.info("修改员工信息，信息为{}",emp.toString());
        if (session.getAttribute("employee") == emp.getId()){
            if (emp.getStatus() == 0){
                return R.error("管理员不能修改自己的账号状态");
            }
        }
        //封装修改信息
        //emp.setUpdateTime(LocalDateTime.now());
        //emp.setUpdateUser((Long) session.getAttribute("employee"));
        employeeService.updateById(emp);
        return R.success("更改成功！");
    }
    
    /**
     * <p>Description: 根据ID查询员工信息</p>
     * 
     * @author liuyiming
     * @date 2022-10-07 16:27
     * @version 1.0.0
     */
    @GetMapping("/{id}")
    public R<Employee> queryById(@PathVariable String id) {
        log.info("根据ID查询员工信息");
        Employee emp = employeeService.getById(id);
        if (emp == null){
            return R.error("暂无此人");
        }
        return R.success(emp);
    }
}
